package com.ktgj.other.task;

import com.ktgj.other.common.TongChengUtils;
import com.ktgj.other.domain.EtOrder;
import com.ktgj.other.domain.EtScenery;
import com.ktgj.other.domain.RcOrder;
import com.ktgj.other.domain.RcOrderTracking;
import com.ktgj.other.domain.common.HttpTongChengInfoIncrement;
import com.ktgj.other.domain.common.RequestBodyIncrement;
import com.ktgj.other.domain.common.SceneryInfoIncrement;
import com.ktgj.other.domain.common.TongChengInfoIncrement;
import com.ktgj.other.mapper.EtOrderMapper;
import com.ktgj.other.mapper.EtSceneryMapper;
import com.ktgj.other.mapper.RcOrderMapper;
import com.ktgj.other.service.IEtSceneryService;
import com.ktgj.other.service.IRcOrderTrackingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class OtherScheduler {

    @Autowired
    private EtOrderMapper etOrderMapper;
    @Autowired
    private EtSceneryMapper etSceneryMapper;
    @Autowired
    private IEtSceneryService etSceneryService;
    @Autowired
    private RcOrderMapper rcOrderMapper;
    @Autowired
    private IRcOrderTrackingService rcOrderTrackingService;

    /**
     * 取消酒店订单
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void cancelTrainOrder() {
        List<EtOrder> etOrderList = etOrderMapper.cancelOrder();
        for (EtOrder etOrder : etOrderList) {
            etOrder.setOrderstatus(40l);
            etOrderMapper.updateEtOrder(etOrder);
        }
    }

    /**
     * 门票更新数据
     * */
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void renew(){
//        int count = 0;
//        HttpTongChengInfoIncrement sceneryIncrementService;
//        do {
//            TongChengUtils tongChengUtils = new TongChengUtils();
//            TongChengInfoIncrement tongChengInfo = new TongChengInfoIncrement();
//            RequestBodyIncrement requestBodyIncrement = new RequestBodyIncrement();
//            requestBodyIncrement.setMaxIncrementId(count);
//            tongChengInfo.setRequestBody(requestBodyIncrement);
//            sceneryIncrementService = tongChengUtils.getSceneryIncrementService(tongChengInfo);
//            count = sceneryIncrementService.getMaxIncrementId();
//            if (sceneryIncrementService.getRespCode() == 1){
//                if (sceneryIncrementService.getSceneryList() != null && sceneryIncrementService.getSceneryList().size() > 0){
//                    for (SceneryInfoIncrement sceneryInfoIncrement : sceneryIncrementService.getSceneryList()) {
//                        if (sceneryInfoIncrement.getIncrementType() == 1){//上架
//                            EtScenery etScenery = new EtScenery();
//                            etScenery.setSceneryid(Long.valueOf(sceneryInfoIncrement.getSceneryId()));
//                            etScenery.setIncrementType("true");
//                            etSceneryMapper.updateIncrementType(etScenery);
//                        }
//                        else if (sceneryInfoIncrement.getIncrementType() == 2){//下架
//                            EtScenery etScenery = new EtScenery();
//                            etScenery.setSceneryid(Long.valueOf(sceneryInfoIncrement.getSceneryId()));
//                            etScenery.setIncrementType("false");
//                            etSceneryMapper.updateIncrementType(etScenery);
//                        }
//                        else if (sceneryInfoIncrement.getIncrementType() == 2){//修改
//                            etSceneryService.save(sceneryInfoIncrement.getSceneryId());
//                        }
//                    }
//                }
//            }
//        } while (sceneryIncrementService.getSceneryId().size() > 0);
//    }

    /**
     * 取消专车订单
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void cancelRcOrder() {
        List<RcOrder> rcOrders = rcOrderMapper.rcOrderList();
        rcOrders.forEach(item -> {
            item.setPaymentstatus(60L);
            item.setOrderstatus(6L);
            item.setUpdateTime(new Date());
            rcOrderMapper.updateRcOrder(item);
            RcOrderTracking orderTracking = new RcOrderTracking();
            orderTracking.setOrderid(Long.valueOf(item.getOrderid()));
            orderTracking.setTrackingnode("取消订单");
            orderTracking.setTrackingcontent("由于您30分钟内未支付，订单已取消。期待下次为您服务。");
            orderTracking.setOpemployeeid(0L);
            orderTracking.setCreateTime(new Date());
            orderTracking.setUpdateTime(new Date());
            orderTracking.setRemark("系统消息");
            rcOrderTrackingService.insertRcOrderTracking(orderTracking);
        });
    }
}
